#!/usr/bin/env python

import rospy
import os
from actions_interface import *
from task_planner.msg import Environment

act = Actions()
i = 0
env_history = [0]

def callback(env):

	i =+ 1
	rospy.loginfo("MANAGE %d ", i)

	if (env != env_history[-1]):
		print " --------------Before---------------- "
		print env
		print act
		print " ------------------------------------ "
		
		# Transition T8
		if (act.a_Manual2Auto == True) and (env.p_Presence == True) and (env.p_SunLight == True):
			act.TurnOnBlinds(env)
			print "T8"

		# Transition T7
		elif (act.a_TurnOnLight == True) and (env.p_Presence == False):
			act.TurnOffLight(env)
			act.Manual2Auto(env)
			print "T7"

		# Transition T6
		elif (act.a_TurnOnLight == True) and (env.p_LightButton == True):
			act.Auto2Manual(env)
			env.p_LightButton = False
			print "T6"

		# Transition T5
		elif (act.a_TurnOnLight == True) and (env.p_BlindsButton == True):
			act.Auto2Manual(env)
			env.p_BlindsButton = False
			print "T5"

		# Transition T4 
		elif (act.a_Manual2Auto == True) and (env.p_Presence == True) and (env.p_SunLight == False):
			act.TurnOnLight(env)
			act.a_Manual2Auto = False
			print "T4"

		# Transition T3
		elif (act.a_Auto2Manual == True) and (env.p_Presence == False):
			act.Manual2Auto(env)
			act.TurnOffLight(env)
			print "T3"

		# Transition T2 
		elif (act.a_TurnOnBlinds == True) and (env.p_BlindsButton == True):
			act.Auto2Manual(env)
			env.p_BlindsButton == False
			print "T2"

		# Transition T1
		elif (act.a_TurnOnBlinds == True) and (env.p_LightButton == True):
			act.Auto2Manual(env)
			env.p_LightButton = False
			print "T1"
		
		env_history.append(env)					# Save the current status of the environment

		print " --------------After---------------- "
		print env
		print act
		print " ----------------------------------- "
 
   
def Manage():

	os.system("clear") 
	rospy.init_node('ManageLightBlinds', anonymous=True)
	print " ManageLightBlinds "
	# mi sottoscrivo al topic chatter, di tipo....
	rospy.Subscriber('/environment/status', Environment, callback)

	rospy.spin()


if __name__ == '__main__':

	Manage()


